package com.econ.powercloud.ems.service.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.econ.powercloud.ems.dal.entity.EmsComputeConfig;
import com.econ.powercloud.ems.dal.mapper.EmsComputeConfigMapper;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Objects;

/**
 * @ Description:
 * @ Author：hgp
 * @ Create:2022-11-25-14:07
 */
@Repository
public class EmsComputeConfigDAO  extends ServiceImpl<EmsComputeConfigMapper,EmsComputeConfig> {
    private final static String LIMIT_ONE = "limit 1";

    public List<EmsComputeConfig> getListByModelAndDayType(List<String> model,Integer dayType) {
        LambdaQueryWrapper<EmsComputeConfig> wrapper = Wrappers.lambdaQuery();
        if (CollectionUtils.isNotEmpty(model)){
            wrapper.in(EmsComputeConfig::getComputeModel,model);
        }
        wrapper.eq(dayType != null,EmsComputeConfig::getDayType,dayType);
        return this.baseMapper.selectList(wrapper);
    }

    public List<EmsComputeConfig> getListByCondition(String stationId, List<String> computeCode, List<String> computeModels
            , Integer dayType, String deviceCode, Integer measureType) {
        LambdaQueryWrapper<EmsComputeConfig> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(!StringUtils.isBlank(stationId), EmsComputeConfig::getEnergyStationId, stationId);
        wrapper.eq(!StringUtils.isBlank(deviceCode), EmsComputeConfig::getDeviceCode, deviceCode);
        wrapper.eq(dayType != null, EmsComputeConfig::getDayType, dayType);
        wrapper.eq(measureType != null, EmsComputeConfig::getMeasureType, measureType);
        if (CollectionUtils.isNotEmpty(computeCode)) {
            wrapper.in(EmsComputeConfig::getComputeCode, computeCode);
        }
        if (CollectionUtils.isNotEmpty(computeModels)) {
            wrapper.in(EmsComputeConfig::getComputeModel, computeModels);
        }
        return this.baseMapper.selectList(wrapper);
    }

    public EmsComputeConfig selectOneByStationIdAndModelAndDayType(String energyStationId, String computeModel, Integer dayType) {
        LambdaQueryWrapper<EmsComputeConfig> wrapper = Wrappers.<EmsComputeConfig>lambdaQuery()
                .eq(EmsComputeConfig::getEnergyStationId, energyStationId)
                .eq(EmsComputeConfig::getComputeModel, computeModel)
                .eq(Objects.nonNull(dayType), EmsComputeConfig::getDayType, dayType)
                .last(LIMIT_ONE);
        return this.getBaseMapper().selectOne(wrapper);
    }
}
